package dao

import (
	"context"
	"fabric-explorer/entity"
	"fabric-explorer/tools"
	"fmt"
	"time"

	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
)

const (
	TRANSACTION_COLLECTION_NAME = "transaction"
)

//插入transaction
func InsertTransaction(transaction entity.Transaction) {
	// tools.InitDB()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	insertResult, err := collection.InsertOne(context.TODO(), transaction)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("Inserted a single document: ", insertResult.InsertedID)
}

//查询list
func FindTransactionList(selecter bson.D) []entity.Transaction {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	cur, err := collection.Find(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
	defer cur.Close(ctx)

	transactionList := make([]entity.Transaction, 0)
	for cur.Next(ctx) {
		transaction := &entity.Transaction{}
		err := cur.Decode(transaction)
		if err != nil {
			fmt.Println(err)
		}
		transactionList = append(transactionList, *transaction)
	}
	if err := cur.Err(); err != nil {
		fmt.Println(err)
	}
	return transactionList
}

//查询Transaction的page
func FindTransactionListPage(selecter bson.D, options *options.FindOptions) []entity.Transaction {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	cur, err := collection.Find(ctx, selecter, options)
	if err != nil {
		fmt.Println(err)
	}
	defer cur.Close(ctx)

	transactionList := make([]entity.Transaction, 0)
	for cur.Next(ctx) {
		transaction := &entity.Transaction{}
		err := cur.Decode(transaction)
		if err != nil {
			fmt.Println(err)
		}
		transactionList = append(transactionList, *transaction)
	}
	if err := cur.Err(); err != nil {
		fmt.Println(err)
	}
	return transactionList
}

//查询一个Transaction
func FindTransactionOne(selecter bson.D) entity.Transaction {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	oneResult := collection.FindOne(ctx, selecter)
	transaction := &entity.Transaction{}
	err := oneResult.Decode(transaction)
	if err != nil {
		fmt.Println(err)
	}
	return *transaction
}

//查询index
func GetTransactionNum(selecter bson.D) int64 {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	num, err := collection.CountDocuments(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
	return num
}

//删除transaction
func DeleteTransaction(selecter bson.D) {
	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
	defer cancel()
	collection := tools.GetCollection(TRANSACTION_COLLECTION_NAME)
	_, err := collection.DeleteMany(ctx, selecter)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println("clear all transaction data")
}
